package FenxiaoService

import (
	"context"
	"gdshop-font-api-go/app/request/FenxiaoReq"
	"gdshop-font-api-go/library/response"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
	"time"
)

func GoodsFollowChange(ctx context.Context, parames *FenxiaoReq.GoodsFollowChange) *response.JsonResponse {
	fuModel, err := GetFenxiaoUserModelByMemberId(ctx, int(parames.MemberId))
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	count, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_goods_follow").Where(
		"fenxiao_user_id = ? AND goods_id = ?", fuModel.Id, parames.GoodsId).Fields("1").Count()
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	if count > 0 {
		_, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_goods_follow").Where(
			"fenxiao_user_id = ? AND goods_id = ?", fuModel.Id, parames.GoodsId).Delete()
		if err != nil {
			return response.FailByRequestMessage(nil, err.Error())
		}
	} else {
		_, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_goods_follow").InsertAndGetId(g.Map{
			"fenxiao_user_id": fuModel.Id,
			"goods_id":        parames.GoodsId,
			"create_at":       time.Now().Unix(),
		})
		if err != nil {
			return response.FailByRequestMessage(nil, err.Error())
		}
	}

	return response.SuccessByRequestMessage(nil, "成功")
}
